package com.daguchuangyi.erp_android.productlist.productsummary;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.manager.ParameterManager;
import com.daguchuangyi.erp_android.model.product.Parameter;
import com.daguchuangyi.erp_android.model.product.Product;
import com.daguchuangyi.erp_android.production_detail.ProductDetailActivity;
import com.daguchuangyi.erp_android.productlist.ProductSummaryManager;
import com.daguchuangyi.erp_android.util.ImageUtil;

import java.util.Set;

/**
 * Created by lijianjian on 2018/3/19.
 */

public class ProductSummaryViewHolder extends BaseViewHolder {

    private ImageView mCoverImageView;
    private ImageView mActionIv;
    private TextView mPNOTv;
    private TextView mDesTv;
    private ImageView mCadIv;

    public void bindProductInfo(RecyclerView.Adapter adapter, Product item) {

        Glide.with(ErpApplication.getAppContext()).load(item.getExhibitionImage())
                .apply(ImageUtil.getProductSummaryOptions())
                .into(mCoverImageView);
        mPNOTv.setText(item.getPNO());

        if (ProductSummaryManager.getInstance().getCheckedProudctIdList().contains(item.getId())) {
            mActionIv.setImageResource(R.drawable.icon_minus);
            mActionIv.setOnClickListener(v -> ProductSummaryManager.getInstance().removeProductChecked(item, true));
        } else {
            mActionIv.setImageResource(R.drawable.icon_plus);
            mActionIv.setOnClickListener(v -> ProductSummaryManager.getInstance().addProductToCheck(item, true));
        }

        String descriptionStr = "";


        Set<Integer> localParams = ParameterManager.getInstance().getLocalParamsCheckedSet();
        if (localParams.contains(ParameterManager.ID_STOCK_OPTIONS)) {
            if (!TextUtils.isEmpty(descriptionStr)) {
                descriptionStr += ("，仓储：" + item.getStorages().size());
            } else {
                descriptionStr = ("仓储：" + item.getStorages().size());
            }
        }
        if (localParams.contains(ParameterManager.ID_SAMPLE_OPTIONS)) {
            if (!TextUtils.isEmpty(descriptionStr)) {
                descriptionStr += ("，样品库：" + item.getShelves().size());
            } else {
                descriptionStr = ("样品库：" + item.getShelves().size());
            }
        }
        for (int i = 0; i < item.getParameters().size(); i++) {
            Parameter productParam = item.getParameters().get(i);
            if (ParameterManager.getInstance().getParametersChecked().contains(productParam)) {
                String value = productParam.getProductparameters().getValue();
                if (TextUtils.isEmpty(value)) {
                    value = " ";
                }
                if (!TextUtils.isEmpty(descriptionStr)) {
                    descriptionStr += ("，" + productParam.getName() + "：" + value);
                } else {
                    descriptionStr = productParam.getName() + "：" + value;
                }
            }
        }
        if (ProductSummaryManager.getInstance().getCurrentState() == ProductSummaryManager.STYLE_LIST) {
            if (localParams.contains(ParameterManager.ID_CAD_IMAGE_OPTIONS)) {
                mCadIv.setVisibility(View.VISIBLE);
                Glide.with(ErpApplication.getAppContext()).load(item.getCassetteImage())
                        .apply(ImageUtil.getProductSummaryOptions())
                        .into(mCadIv);
            } else {
                mCadIv.setVisibility(View.GONE);
            }

        } else {
            mCadIv.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(item.getImageId()) || TextUtils.isEmpty(item.getExhibitionImage())) {
            mPNOTv.setTextColor(itemView.getContext().getResources().getColor(R.color.no_img_product_PNO_color));
        } else {
            mPNOTv.setTextColor(itemView.getContext().getResources().getColor(R.color.normal_product_PNO_color));
        }
        mDesTv.setText(descriptionStr);
        itemView.setOnClickListener(view -> ProductDetailActivity.start(view.getContext(), item.getId()));
    }

    public ProductSummaryViewHolder(View view) {
        super(view);
        mCoverImageView = view.findViewById(R.id.product_cover_iv);
        mActionIv = view.findViewById(R.id.product_action_iv);
        mDesTv = view.findViewById(R.id.product_desc_tv);
        mPNOTv = view.findViewById(R.id.product_num_tv);
        mCadIv = view.findViewById(R.id.product_cad_iv);
    }
}
